
# 变量定义
BINARY_NAME=backend
BUILD_DIR=build
BACKEND_MAIN=backend/cmd/main.go

# Go 相关命令
GO=go
GOFLAGS=-v
GOBUILD=$(GO) build $(GOFLAGS)
GOTEST=$(GO) test $(GOFLAGS)
GOCLEAN=$(GO) clean

# 默认目标
.DEFAULT_GOAL := help

# 帮助信息
.PHONY: help
help:
	@echo "使用方法:"
	@echo "  make [target]"
	@echo ""
	@echo "可用目标:"
	@echo "  build         - 构建后端应用"
	@echo "  run           - 运行后端应用"
	@echo "  clean         - 清理构建文件"
	@echo "  test          - 运行测试"
	@echo "  deps          - 下载依赖"
	@echo "  fmt           - 格式化代码"
	@echo "  vet           - 运行 go vet"
	@echo "  all           - 运行 clean, fmt, vet, build, test"


# 生成 Swagger 文档
.PHONY: swagger
swagger:
	@echo "生成 Swagger 文档..." 
	@swag init -g backend/cmd/main.go -o backend/docs --parseDependency --parseInternal --templateDelims "{{,}}"
	@sed -i '' '/LeftDelim/d; /RightDelim/d' backend/docs/docs.go

# tidy
.PHONY: tidy
tidy:
	@echo "tiding"
	@go mod tidy

# 清理构建文件
.PHONY: clean
clean:
	@echo "清理构建文件..."
	@rm -rf $(BUILD_DIR)
	@$(GOCLEAN)

# 构建后端
.PHONY: build
build: deps tidy swagger
	@echo "构建后端应用..."
	@mkdir -p $(BUILD_DIR)
	@CGO_ENABLED=1 $(GOBUILD) -o $(BUILD_DIR)/$(BINARY_NAME) $(BACKEND_MAIN)

# 运行后端
.PHONY: run
run: build
	@echo "运行后端应用..."
	@cd $(BUILD_DIR) && ./$(BINARY_NAME)

# 运行测试
.PHONY: test
test:
	@echo "运行测试..."
	@$(GOTEST) ./...

# 下载依赖
.PHONY: deps
deps:
	@echo "下载依赖..."
	@$(GO) mod download

# 格式化代码
.PHONY: fmt
fmt:
	@echo "格式化代码..."
	@$(GO) fmt ./...

# 运行 go vet
.PHONY: vet
vet:
	@echo "运行 go vet..."
	@$(GO) vet ./...

# 运行所有任务
.PHONY: all
all: clean fmt vet build test
	@echo "所有任务完成"